var shadowRoot;
var arr;
var titles;
function save() {
    shadowRoot = document.querySelector("#app > div > div.app_content > div:nth-child(4) > micro-app").shadowRoot;
    titles = shadowRoot.querySelectorAll('.input_title');
    arr = [];

    // 遍历每个元素
    xgetval("商品标题", "input.weui-desktop-form__input");
    xget("类别",".title_category");
    xgetval("商品详情", "textarea.weui-desktop-form__textarea");
    xgetpro(".goodsParams", ".goodsParams_key input", ".goodsParams_value input");
    // xgettext(".module-label-item",/([^<>]*)：<span>([^<>]*)<\/span>/);
    xgetpros("商品属性",".goods-attr-item .flex-row","input","value","img","src")
    xgetimgs("商品图片和视频", ".goods_upload .draggable_content");
    xgetimgs("商品详情图集",".goods_details");

    //商品参数
    let area=findParentChildText(shadowRoot,".default_box",".box_title","商品参数");
    let arr2=area.querySelectorAll("input.weui-desktop-form__input");
    let data=[];
    for (let i = 0; i < arr2.length; i++) {
        if(arr2[i].hasAttribute("disabled")){
            data.push(arr2[i].value);
        }
    }
    arr.push({name: "商品参数", value: data.join(",")});

    //商品规格
    s="";
    area=shadowRoot.querySelector(".default_box.goods-sale-sku");
    skus=area.querySelectorAll(".vue-recycle-scroller__item-view");
    let img;
    for (let i = 0; i < skus.length; i++) {
        temp= skus[i].querySelector("div.listItem").getAttribute("data-key");
        proName=temp.substring(0,temp.indexOf("."));
        guige = skus[i].querySelector(".property_entry_item_info").textContent;
        img = skus[i].querySelector("img").getAttribute("src");
        style_widths = skus[i].querySelectorAll(".listItem_bottom>.style_width");
        soujia = style_widths[0].querySelector("input").value;
        kuchun = style_widths[1].querySelector("input").value;
        bianma = style_widths[2].querySelector("input").value;
        s+=proName+"#"+guige+"#"+img+"#"+soujia+"#"+kuchun+"#"+bianma+",";
    }
    arr.push({name: "商品规格", value: s.substring(0,s.length-1)});


    //售后说明
    area=findParentChildText(shadowRoot,".default_box",".input_title","售后说明");
    const address=area.querySelector("input.weui-desktop-form__input[placeholder=选择或搜索售后地址]").value;
    arr.push({name: "售后地址", value: address});
    const shsm = area.querySelector("textarea.weui-desktop-form__textarea[placeholder=填写说明，不超过200字]")?.value;
    arr.push({name: "售后说明", value: shsm});


     //xgetimgs("质检报告",".otherInfo_wrp form>div[0]");
     //xgetimgs("报关单",".otherInfo_wrp form>div[1]");
     //xgetimgs("委托进口协议",".otherInfo_wrp form>div[2]");
    titles.forEach(function (title) {
        title.textContent = title.textContent.replace(/\(选填\)/g, "");
        title.textContent = title.textContent.replace(/\(|\)/g, "");
        p = title.closest(".flex-1");
        if (title.textContent != "" && p != null) {
            el = p.querySelector("input");
            if (el != null) {
                let value = el.value;
                if (title != null && el != null) {
                    value = value.replace(/[0-9]+\/[0-9]+/g, "");
                    value = value.replace(/\*|^\s*|\s*$/g, "");
                    console.log(title.textContent + ":" + value);
                    arr.push({name: title.textContent, value: value});
                }
            }
            el = p.querySelector("span.weui-desktop-form__dropdown__value");
            if (el != null) {
                let value = el.textContent;
                if (title != null && el != null) {
                    value = value.replace(/[0-9]+\/[0-9]+/g, "");
                    value = value.replace(/\*|^\s*|\s*$/g, "");
                    console.log(title.textContent + ":" + value);
                    arr.push({name: title.textContent, value: value});
                }
            }

        }


        p = closet(title, "edit-row");
        if (title.textContent != "" && p != null) {
            el = p.querySelectorAll("input,textarea,img,.multiple-scroll-selector-tag");
            value = "";
            for (let i = 0; i < el.length; i++) {
                if (title != null && el[i] != null) {
                    if (el[i].tagName == "INPUT" || el[i].tagName == "TEXTAREA") {
                        value = value + "," + el[i].value.replace(/[0-9]+\/[0-9]+/g, "");
                        value = value.replace(/\*|^\s*|\s*$/g, "").replace(/^\s*/g, "");
                    } else if (el[i].tagName == "IMG") {
                        el.forEach(el2 => {
                            value = value + "," + el2.getAttribute("src");
                        })
                    } else if (el[i].classList.contains('multiple-scroll-selector-tag')) {
                        el.forEach(el2 => {
                            value = value + "," + el2.querySelector("span").textContent;
                        })
                    }
                }
            }
            value = value.replace(/^,/, "");
            console.log(title.textContent + ":" + value);
            arr.push({name: title.textContent, value: value});
        }

    });

    const queryString = window.location.search;
    const params = new URLSearchParams(queryString);
    const item_id = params.get('item_id');
    const url = window.location.href;
    fetch('https://shop.shante.me/yunshop/au3/saveprotype.php?item_id=' + item_id + "&XDEBUG_SESSION_START=54543", {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({url: url, item_id: item_id, arr: arr})
    })
        .then(response => response.json())
        .then(data => {
            alert('保存成功！')
        })
        .catch(error => console.error(error));
}

function closet(el, className) {
    while ((el = el.parentElement) && el.nodeType === 1) {
        if (el.classList.contains(className)) {
            return el;
        }
    }
    return null;
}

function xget(xname,selector){
    let textarea=shadowRoot.querySelector(selector).textContent.replace(/\s*| */g,"");
    arr.push({name:xname,value:textarea});
}

function xgetval(xname,selector){
    let textarea=shadowRoot.querySelector(selector).value;
    arr.push({name:xname,value:textarea});
}

function xgetpro(p,xname,xvalue){
    p=shadowRoot.querySelectorAll(p);
    p.forEach(function(o){
        if(o.querySelector(xname)){
            n=o.querySelector(xname).value;
            v=o.querySelector(xvalue).value;
            arr.push({name:n,value:v});
        }
    })
}

function xgetpros(name,item,proName,proNameF,proImg,proImgF){
    items=shadowRoot.querySelectorAll(item);
    s="";
    items.forEach(function(o){
        if(o.querySelector(proName)){
            a=o.querySelector(proName);
            b=o.querySelector(proImg);
            s+=proName+"#";
            if(eval("a."+proNameF)!="") {
                s += eval("a." + proNameF) ;
            }
            s+="#";
            if(b!=""&&b!=null) {
                s += eval("b." + proImgF) + ","
            }
        }
    })
    arr.push({name:name,value:s});
}

function xgettext(selector,re){
    a=shadowRoot.querySelectorAll(selector);
    a.forEach(function(o){
        s=o.innerHTML;
        result=s.match(re);
        arr.push({name:result[1],value:result[2]});
    })
}

function xgetimgs(xname,selector){
    o=shadowRoot.querySelector(selector);
    a=o.querySelectorAll("div.margin_bottom.cursor_pointer img");
    v="";
    a.forEach(function(o){
        v+=o.getAttribute("src")+",";
    })
    arr.push({name:xname,value:v});
}

function xgetimgs(xname,selector){
    o=shadowRoot.querySelector(selector);
    a=o.querySelectorAll("img.baseInner_content_img");
    v="";
    a.forEach(function(o){
        v+=o.getAttribute("src")+",";
    })
    arr.push({name:xname,value:v});
}